package cn.mumzone.pos.ui.widgets.schedule;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Calendar;

import cn.mumzone.basic.adapter.rv.CommonAdapter;
import cn.mumzone.basic.adapter.rv.ItemViewDelegate;
import cn.mumzone.basic.adapter.rv.ViewHolder;
import cn.mumzone.basic.util.AppDateUtil;
import cn.mumzone.basic.util.StringUtils;
import cn.mumzone.pos.R;

/**
 * Created by qiuzhenhuan on 2019/7/10.
 */


public class ADA_Schedule extends CommonAdapter<ScheduleEntity> {

    private static final int VIEW_TYPE_DEFAULT = 0;
    private static final int VIEW_TYPE_EMPTY = 1;


    private int checkPosition = -1;

    private boolean notScheduleEnable = true;

    private ViewHolder mPreHolder;


    public ADA_Schedule(Context context) {
        super(context);
        this.mContext = context;

        addItemViewDelegate(VIEW_TYPE_EMPTY, new ItemViewDelegate<ScheduleEntity>() {
            @Override
            public int getItemViewLayoutId() {
                return R.layout.item_schedule_empty;
            }

            @Override
            public boolean isForViewType(ScheduleEntity item, int position) {
                return true;
            }

            @Override
            public void convert(ViewHolder holder, ScheduleEntity scheduleEntity, int position) {

            }
        });
    }

    @Override
    protected int itemLayoutId() {
        return R.layout.item_schedule;
    }


    @Override
    protected void convert(final ViewHolder holder, ScheduleEntity scheduleEntity, final int position) {

        if (getItemViewType(position) == VIEW_TYPE_DEFAULT) {
            final ScheduleEntity entity = getDataList().get(position);
            Calendar calendar = Calendar.getInstance();
            calendar.clear();
            calendar.setTimeInMillis(AppDateUtil.getStringToDate(entity.getDate(), "yyyy-MM-dd"));
            int day = calendar.get(Calendar.DAY_OF_MONTH);
            RadioButton rbDay = holder.getView(R.id.rb_day);
            rbDay.setText(String.valueOf(day));
            if ((!notScheduleEnable && !entity.isScheduled()) || AppDateUtil.compareDateIsLessThanNow(calendar.get(Calendar.YEAR),
                    calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.DAY_OF_MONTH))) {
                rbDay.setEnabled(false);
                rbDay.setChecked(false);
            } else {
                rbDay.setEnabled(true);
                rbDay.setChecked(entity.isChecked());
            }

            if (entity.isChecked()) {
                checkPosition = position;
                mPreHolder = holder;
                holder.setImageResource(R.id.iv_status, R.drawable.svg_ic_schedule_checked_cur_day);
            } else {
                holder.setImageResource(R.id.iv_status, R.drawable.svg_ic_schedule_checked);

            }

            holder.setVisible(R.id.iv_status, entity.isScheduled());

            rbDay.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // 点击同一条目不做响应
                    if (mPreHolder == holder)
                        return;

                    if (mPreHolder == null) {
                        mPreHolder = holder;
                        ScheduleEntity entity = getDataList().get(position);
                        entity.setChecked(true);
                        mPreHolder.setImageResource(R.id.iv_status, R.drawable.svg_ic_schedule_checked_cur_day);
                        mPreHolder.setChecked(R.id.rb_day, true);
                        if (onItemClickListener != null) {
                            onItemClickListener.onItemClicked(position);
                        }
                        return;
                    }


                    ScheduleEntity preCheckEntity = getDataList().get(mPreHolder.getAdapterPosition());
                    preCheckEntity.setChecked(false);
                    mPreHolder.setChecked(R.id.rb_day, false);
                    mPreHolder.setImageResource(R.id.iv_status, R.drawable.svg_ic_schedule_checked);


                    ScheduleEntity checkEntity = getDataList().get(holder.getAdapterPosition());
                    checkEntity.setChecked(true);
                    holder.setChecked(R.id.rb_day, true);
                    holder.setImageResource(R.id.iv_status, R.drawable.svg_ic_schedule_checked_cur_day);

                    mPreHolder = holder;
                    if (onItemClickListener != null) {
                        onItemClickListener.onItemClicked(position);
                    }
                }
            });
        }
    }


    public void setNotScheduleEnable(boolean notScheduleEnable) {
        this.notScheduleEnable = notScheduleEnable;
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        ScheduleEntity entity = getDataList().get(position);
        return StringUtils.isEmpty(entity.getDate()) ? VIEW_TYPE_EMPTY : VIEW_TYPE_DEFAULT;
    }

    public void clearCheck() {
        if (mPreHolder != null) {
            ScheduleEntity checkEntity = getDataList().get(mPreHolder.getAdapterPosition());
            checkEntity.setChecked(false);
            mPreHolder.setChecked(R.id.rb_day, false);
            mPreHolder = null;
        }
    }

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClicked(int position);
    }

}

